// export const URL = 'https://www.shlxkj.info/API/api'
 export const URL = 'https://yun.shlxkj.info:44306/api' 
 // export const URL ='http://192.168.3.12:9010/api'
import store from '@/store'
const params = (url, data) => {
	url += '?'
	const keys = Object.keys(data)
	for (const key of keys) {
		url += `${key}=${encodeURIComponent(typeof data[key] === 'string' ? data[key].trim() : data[key])}&`
	}
	return url.substring(0, url.length - 1)
}

const request = ({ method, url, data = {} }) => {
	return new Promise((resolve, reject) => {
		console.log('url: ', params(url, data))
		uni.request({
			url: method === 'GET' ? params(url, data) : url,
			method,
			data,
			header: {
				 // "Content-Type": "application/x-www-form-urlencoded",
				// Authorization: 'Bearer ' + store.state.vx_token,
				// ApiKey: store.state.vx_token
			},
			success: (res) => {
				if(res.data.code!=200){
					uni.showToast({
						title: `${res.data.message}`,
						icon: 'none',
						duration: 3000
					})
				}
				// else if(res.data.code==801){
				// 	uni.showToast({
				// 		title: `${res.data.message}`,
				// 		icon: 'none',
				// 		duration: 3000
				// 	})
				// }else if(res.data.code==501){
				// 	uni.showToast({
				// 		title: `${res.data.message}`,
				// 		icon: 'none',
				// 		duration: 3000
				// 	})
				// }
				
				// if (res.statusCode === 200) {
				// 	if (res.data.Code !== 200) {
				// 		let pages = getCurrentPages()
				// 		let current = pages[pages.length - 1]
				// 		if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
				// 			return resolve(res.data)
				// 		}
				// 		reject(res.data)
				// 	}
				// 	resolve(res.data ? res.data : res)
				// } 
				else if (res.data.code === 401) {
					store.commit('$uStore', { name: 'vx_token', value: '' })
					// let pages = getCurrentPages()
					// let current = pages[pages.length - 1]
					// if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
					// 	return resolve(res.data)
					// }
					uni.showToast({ title: '需重新登录' })
					uni.navigateTo({ url: '/pages/login/index' })
				// } else {
				// 	uni.showToast({ title: res.data.msg })
				// 	reject(res.data)
				}
				const data = res.data

				if (data) {
					resolve(data)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				if (err.statusCode === 401) {
					uni.redirectTo({ url: '/pages/login/index' })
				}
				reject(err)
			}
		})
	})
}

// 登录
export function login (data) {
	return request({
		method: 'POST',
		url: `${URL}/User/Login`, 
		data
	})
}

// 获取手机号码
export function getPhone (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/decryptData`,
		data
		}
	)
}

// 注册
export function register (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/Login`,
		data
	})
}

// 用户协议
export function GetUserPrivacyAgreement (data) {
	return request({
		method: 'GET',
		url: `${URL}/User/GetUserPrivacyAgreement`,
		data
	})
}

//注册
export function InsertUser (data) {
	return request({
		method: 'POST',
		url: `${URL}/User/InsertUser`,
		data
	})
}
//家属绑定患者
export function BindPatientRequest (data) {
	return request({
		method: 'POST',
		url: `${URL}/User/BindPatientRequest`,
		data
	})
}
//查询心率，位置
export function FindHeartRateAndLocation (data) {
	return request({
		method: 'POST',
		url: `${URL}/Device/FindHeartRateAndLocation`,
		data
	})
}
//往期心率 
export function PaginFindHeartRateAndLocationByTimeRange (data) {
	return request({
		method: 'POST',
		url: `${URL}/Device/PaginFindHeartRateAndLocationByTimeRange`,
		data
	})
}
//七天步数 
export function FindUserSteps (data) {
	return request({
		method: 'POST',
		url: `${URL}/Device/FindUserSteps`,
		data
	})
}
//异常状态判定 
export function AddUserConfigurationInformation (data) {
	return request({
		method: 'POST',
		url: `${URL}/User/AddUserConfigurationInformation`,
		data
	})
}
